﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using ChinaSoft.BLL;
using ChinaSoft.Model;

namespace Admin.Sys.UserGroup
{
    public partial class UserGroup_Edit : System.Web.UI.Page
    {
        private BLL_T_SYS_GROUP bllGroup = new BLL_T_SYS_GROUP();
        private Model_T_SYS_GROUP modelGroup = new Model_T_SYS_GROUP();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTreeView();
                ControlBtn(false, false, false, false, false);
                ControlTextBox(true, true, true);
                BindArea();
            }
        }
        /// <summary>
        /// 控制控件的状态（禁用、启用）
        /// </summary>
        /// <param name="btnAdd"></param>
        /// <param name="btnDel"></param>
        /// <param name="btnUpdate"></param>
        /// <param name="btnSave"></param>
        /// <param name="btnCancel"></param>
        private void ControlBtn(bool btnAdd, bool btnDel, bool btnUpdate, bool btnSave, bool btnCancel)
        {
            imgBtnAdd.Enabled = btnAdd;
            imgBtnDel.Enabled = btnDel;
            imgBtnUpdate.Enabled = btnUpdate;
            imgBtnSave.Enabled = btnSave;
            imgBtnCancel.Enabled = btnCancel;
        }
        /// <summary>
        /// 绑定树性菜单
        /// </summary>
        private void BindTreeView()
        {
            TreeView1.Nodes.Clear();
            DataSet ds = bllGroup.GetAllList();
            ChinaSoft.BLL.Common.TreeViewOperate bllTreeView = new ChinaSoft.BLL.Common.TreeViewOperate();
            bllTreeView.BindTreeView(ds, TreeView1, "NAME", "SYS_GROUP_ID", "PARENT_ID", false);
        }
        /// <summary>
        /// 当树形菜单选择改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            string groupId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            txtName.Value = TreeView1.SelectedNode.Text;
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            txtAreaName.Value = ChinaSoft.Common.Input.SetString(bllBind.GetFiledContent("AREA_NAME", "t_sys_group", "where sys_group_id=" + groupId));
            ControlTextBox(true, true, false);
            HiddenGroupId.Value = groupId;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true,false, true, true);
            ControlTextBox(false, false, true);
            HiddenGroupId.Value = "";
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            string groupId = ChinaSoft.Common.Input.SetString(TreeView1.SelectedNode.Value);
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            bool result = bllBind.DeleteWhere("T_SYS_GROUP", "sys_group_id=" + groupId);
            if (result)
            {
                ChinaSoft.Common.MessageBox.Show(this, "删除成功！");
                ControlTextBox(false, false, true);
                BindTreeView();
                HiddenGroupId.Value = "";
            }
            else
            {
                ChinaSoft.Common.MessageBox.Show(this, "该数据已经不存在");
                BindTreeView();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnUpdate_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, false, true, true);
            ControlTextBox(false, false, false);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            if (HiddenGroupId.Value == "")
            {
                #region 添加
                modelGroup.SYS_GROUP_ID = Guid.NewGuid().ToString();
                modelGroup.AREA_ID = HiddenAreaId.Value;
                modelGroup.AREA_NAME = txtAreaName.Value.Trim();
                modelGroup.CREATE_TIME = DateTime.Now;
                modelGroup.CREATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelGroup.CREATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                modelGroup.DECODE = "utf-8";
                modelGroup.LEVEL_NO = TreeView1.SelectedNode.Depth;
                modelGroup.NAME = txtName.Value.Trim();
                modelGroup.PARENT_ID = ChinaSoft.Common.Input.SetString(HiddenFatherId.Value);
                modelGroup.UPDATE_TIME = DateTime.Now;
                modelGroup.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                modelGroup.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                bllGroup.Add(modelGroup);
                #endregion
                ChinaSoft.Common.MessageBox.Show(this, "添加成功！");
                BindTreeView();
                ControlTextBox(true, true, true);
            }
            else
            {
                List<Model_T_SYS_GROUP> modelList = bllGroup.GetModelList(" SYS_GROUP_ID=" + HiddenGroupId.Value);
                if (modelList != null)
                {
                    modelGroup = modelList[0];
                    #region 更新
                    modelGroup.AREA_ID = HiddenAreaId.Value;
                    modelGroup.AREA_NAME = txtAreaName.Value.Trim();

                    modelGroup.LEVEL_NO = TreeView1.SelectedNode.Depth;
                    modelGroup.NAME = txtName.Value.Trim();
                    modelGroup.PARENT_ID = ChinaSoft.Common.Input.SetString(HiddenFatherId.Value);
                    modelGroup.UPDATE_TIME = DateTime.Now;
                    modelGroup.UPDATOR_ID = ChinaSoft.Common.Input.SetString(Session["currentUserID"]);
                    modelGroup.UPDATOR_NAME = ChinaSoft.Common.Input.SetString(Session["UserName"]);
                    bllGroup.Update(modelGroup);

                    #endregion
                    ChinaSoft.Common.MessageBox.Show(this, "更新成功！");
                    BindTreeView();
                }
                else
                {
                    ChinaSoft.Common.MessageBox.Show(this, "该数据已经不存在！");
                }
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgBtnCancel_Click(object sender, ImageClickEventArgs e)
        {
            ControlBtn(true, true, true, false, false);
            ControlTextBox(true, true, true);
        }

        /// <summary>
        /// 控制文本框
        /// </summary>
        /// <param name="txtNameDisabled"></param>
        /// <param name="txtAreaNameDisabled"></param>
        /// <param name="boolClear"></param>
        private void ControlTextBox(bool txtNameDisabled, bool txtAreaNameDisabled,bool boolClear)
        {
            if (boolClear)
            {
                txtName.Value = "";
                txtAreaName.Value = "";
            }
            if (txtNameDisabled)
                txtName.Attributes.Add("readyonly", "");
            txtName.Disabled = txtNameDisabled;
            
            txtAreaName.Disabled = txtAreaNameDisabled;
            if (txtAreaNameDisabled)
                txtAreaName.Attributes.Add("readyonly", "");
        }

        /// <summary>
        /// 绑定区域
        /// </summary>
        private void BindArea()
        {
            TreeView2.Nodes.Clear();
            ChinaSoft.BLL.Common.AreaTreeView bllAreaTreeView = new ChinaSoft.BLL.Common.AreaTreeView();
            bllAreaTreeView.BindArea(TreeView2);
        }
        protected void TreeView2_SelectedNodeChanged(object sender, EventArgs e)
        {
            int areaId= ChinaSoft.Common.Input.SetInt(TreeView2.SelectedNode.Value);
            HiddenAreaId.Value = ChinaSoft.Common.Input.SetString(TreeView2.SelectedNode.Value);
            HiddenAreaName.Value = TreeView2.SelectedNode.Text;
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            HiddenFatherId.Value = ChinaSoft.Common.Input.SetString(bllBind.GetFiledContent("parent_id", "t_sys_area", "where sys_area_id=" + areaId));
        }

    }
}